package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper1;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper1 setmealMapper1;
    /**
     * 购物车添加
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result add(ShoppingCartDTO dto) {
        //非空判断
        if(dto==null){
        return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //查询数据库是否存在菜品或者套餐
        //进行映射
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        //写入shoppingCart中储存
        shoppingCart.setUserId(currentId);

        shoppingCart = shoppingCartMapper.queryShoppingCar(shoppingCart);
        //添加数量
        if(shoppingCart!=null){
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }
        if(shoppingCart==null){
            //不存在就添加
            shoppingCart=new ShoppingCart();
            shoppingCart.setUserId(currentId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            //添加菜品
            if (dto.getDishId()!=null) {
                //设置id和口味
                shoppingCart.setDishId(dto.getDishId());
                shoppingCart.setDishFlavor(dto.getDishFlavor());
                //获取金钱图片名字
                Dish byId = dishMapper.getById(dto.getDishId());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setName(byId.getName());
                shoppingCart.setAmount(byId.getPrice());
            }
            //添加套餐
            if(dto.getSetmealId()!=null){
                shoppingCart.setSetmealId(dto.getSetmealId());
                //获取金钱图片名字
                Setmeal setmeal = setmealMapper1.getByIdWithDish(dto.getSetmealId());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //添加到购物车调用接口mapper
            shoppingCartMapper.insert(shoppingCart);
        }
        //返回
        return Result.success();
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public Result list() {
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        //调用mapper
        List<ShoppingCart> carts = shoppingCartMapper.lsit(currentId);
        //返回
        return Result.success(carts);
    }

    /**
     * 删除购物车数据
     *
     * @return
     */
    @Override
    @Transactional
    public Result clean() {
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        //调用mapper
        shoppingCartMapper.clean(currentId);
        //返回
        return Result.success();
    }

    /**
     * 删除一个购物车数据
     *
     * @return
     */
    @Override
    public Result deleteOne(ShoppingCart dto) {
        //非空判断
        if(dto==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //获取用户ID
        Long currentId = BaseContext.getCurrentId();
        dto.setUserId(currentId);
        //查询购物车
        List<ShoppingCart> lsit = shoppingCartMapper.lsit(currentId);
        if(lsit !=null && lsit.size()>0){
            for (ShoppingCart cart : lsit) {
                if(cart.getDishId()==dto.getDishId() || cart.getSetmealId()==dto.getSetmealId()){
                    //判断数量是大于一还是等于一
                    if (cart.getNumber()==1){
                        //当前商品在购物车中的份数为1，直接删除当前记录
                        shoppingCartMapper.delete(cart.getId());
                        break;
                    }else {
                        //当前商品在购物车中的份数不为1，减1
                        cart.setNumber(cart.getNumber()-1);
                        shoppingCartMapper.updateNumberById(cart);
                        break;
                    }
                }
            }
        }
        //返回
        return Result.success();
    }
}
